package com.advanced.homework0526;
//2:控制对象创建的个数:有一个类,假设叫Credidate,限制只能创建50个对象,

//如果超过50个 ,则抛出异常,如何实现(记住,必须在类内部控制,外部控制不能算,提示,用static控制)

public class Credidate {

	private static int COUNT = 0;
	private final static int LIMIT = 50;

	public static Credidate getCredidate() throws Exception {
		if (COUNT < LIMIT) {
			COUNT++;
			return new Credidate();
		} else {
			throw new Exception("最多只能创建50个对象！");
		}
	}

	private Credidate() {

	}

	public static void main(String[] args) {
		try {
			for (int i = 1; i < 55; i++) {
				System.out.println("创建第" + i + "个对象：" + getCredidate());
			}
		} catch (Exception e) {
			System.err.println("捕捉到异常！");
			e.printStackTrace();
		}
	}
}
